package com.yanqu.road.server.timer;

import com.yanqu.road.server.manager.cricketcourt.timer.CrossCricketCourtTask;
import com.yanqu.road.server.manager.kuangmatch.task.KuangCheckMatchGroupTask;
import com.yanqu.road.server.manager.kuangmatch.task.KuangMatchGroupSyncTask;
import com.yanqu.road.server.timer.dataanalytics.Cross3DataAnalyticsTask;
import com.yanqu.road.server.timer.log.CrossAutoLogSaveTask;
import com.yanqu.road.server.timer.marriage.CrossWorldMarriagePublishUploadTask;
import com.yanqu.road.server.timer.systemgroup.CrossSystemGroupTask;
import com.yanqu.road.server.timer.union.*;
import com.yanqu.road.task.CheckDeadLockTask;
import com.yanqu.road.task.DirectMemoryMonitorTask;
import com.yanqu.road.server.TempServer;
import com.yanqu.road.server.timer.shuihuyezicard.ShuiHuYeZiCardAutoPlayCardTask;
import com.yanqu.road.server.timer.shuihuyezicard.ShuiHuYeZiCardMatchTask;
import com.yanqu.road.server.timer.uniontrade.CrossUnionTradeTask;
import com.yanqu.road.utils.executor.GeneralTask;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 所有的定时器写在这边
 */
public class CenterCross2TimerMgr {

    public static final int ALL_SYNC_TIME = 300000;      //5min全同步

    private static final Logger logger = LogManager.getLogger(CenterCross2TimerMgr.class.getName());

    private static ScheduledExecutorService timerExecutorService;

    private static MgrDataSaveTask mgrDataSaveTask = null;

    private static ConnectionCheckTask connectionCheckTask = null;

    private static DirectMemoryMonitorTask directMemoryMonitorTask;

    private static CmdStatisticsTask cmdStatisticsTask;

    private static ShuiHuYeZiCardMatchTask shuiHuYeZiCardMatchTask;
    private static ShuiHuYeZiCardAutoPlayCardTask shuiHuYeZiCardAutoPlayCardTask;

    private static LogDataSaveCrossCenter2Task logDataSaveCrossCenter2Task;

    private static CrossUnionDailyResetTask unionDailyResetTask = null;

    private static CrossUnionFlagTask unionFlagTask = null;

    private static CrossUnionTradeTask unionTradeTask = null;      //商贸路线定时检测

    private static CrossUnionHuaBenWeekTask huaBenWeekTask = null;

    private static CrossServerListMgrReloadTask serverListMgrReloadTask = null;

    private static CrossUnionSyncTask crossUnionSyncTask = null;

//    private static CrossGoodsConfigReloadTask goodsConfigReloadTask = null;

    private static CrossUnionRankMsgOverTask crossUnionRankMsgOverTask = null;
    private static CrossMonthAbilityRankUploadNotifyTask crossMonthAbilityRankUploadNotifyTask = null;

    private static CrossUnionActivityGroupCheckTask crossUnionActivityGroupCheckTask;
    private static CrossUnionCampWarGroupCheckTask crossUnionCampWarGroupCheckTask;
    private static CrossUnionCampWarSyncTask crossUnionCampWarSyncTask;

    private static CrossCricketCourtTask crossCricketCourtTask = null;

    private static CrossSystemLoopTask crossSystemLoopTask = null;

    private static KuangMatchGroupSyncTask kuangMatchGroupSyncTask = null;

    private static KuangCheckMatchGroupTask kuangCheckMatchGroupTask = null;

    private static CrossWorldMarriagePublishUploadTask marriagePublishUploadTask = null;

    private static CrossSystemGroupTask systemGroupTask = null;

    private static Cross3DataAnalyticsTask cross3DataAnalyticsTask = null;

    private static CheckDeadLockTask checkDeadLockTask = null;

    private static CrossAutoLogSaveTask crossAutoLogSaveTask = null;

    public static synchronized boolean initGeneralTimers(int serverType) {
        timerExecutorService = TempServer.getTimerExecutorService();

        if (null == mgrDataSaveTask) {
            mgrDataSaveTask = new MgrDataSaveTask();
            timerExecutorService.scheduleWithFixedDelay(mgrDataSaveTask, 0, 60, TimeUnit.SECONDS);
        }


        if (null == connectionCheckTask) {
            connectionCheckTask = new ConnectionCheckTask(serverType);
            timerExecutorService.scheduleWithFixedDelay(connectionCheckTask, 5, 5, TimeUnit.SECONDS);
        }

        if (directMemoryMonitorTask == null) {
            directMemoryMonitorTask = new DirectMemoryMonitorTask();
            timerExecutorService.scheduleWithFixedDelay(directMemoryMonitorTask, 60, 60, TimeUnit.SECONDS);
        }

        if (cmdStatisticsTask == null) {
            cmdStatisticsTask = new CmdStatisticsTask();
            timerExecutorService.scheduleWithFixedDelay(cmdStatisticsTask, 60, 30, TimeUnit.SECONDS);
        }

        if (shuiHuYeZiCardMatchTask == null) {
            shuiHuYeZiCardMatchTask = new ShuiHuYeZiCardMatchTask();
            timerExecutorService.scheduleWithFixedDelay(shuiHuYeZiCardMatchTask, 30, 1, TimeUnit.SECONDS);
        }
        if(logDataSaveCrossCenter2Task == null){
            logDataSaveCrossCenter2Task = new LogDataSaveCrossCenter2Task();
            timerExecutorService.scheduleWithFixedDelay(logDataSaveCrossCenter2Task, 60, 60, TimeUnit.SECONDS);
        }
        if (shuiHuYeZiCardAutoPlayCardTask == null) {
            shuiHuYeZiCardAutoPlayCardTask = new ShuiHuYeZiCardAutoPlayCardTask();
            timerExecutorService.scheduleWithFixedDelay(shuiHuYeZiCardAutoPlayCardTask, 30, 500, TimeUnit.MILLISECONDS);
        }

        if (unionFlagTask == null) {
            unionFlagTask = new CrossUnionFlagTask();
            timerExecutorService.scheduleWithFixedDelay(unionFlagTask, 0, 10, TimeUnit.SECONDS);
        }

        if (null == unionDailyResetTask) {
            unionDailyResetTask = new CrossUnionDailyResetTask();
            timerExecutorService.scheduleWithFixedDelay(unionDailyResetTask, 5, 1, TimeUnit.SECONDS);
        }

        if (null == unionTradeTask) {
            unionTradeTask = new CrossUnionTradeTask();
            timerExecutorService.scheduleWithFixedDelay(unionTradeTask, 15, 10, TimeUnit.SECONDS);
        }

        if (null == huaBenWeekTask) {
            huaBenWeekTask = new CrossUnionHuaBenWeekTask();
            timerExecutorService.scheduleWithFixedDelay(huaBenWeekTask, 15, 5, TimeUnit.SECONDS);
        }

        if (null == serverListMgrReloadTask) {
            serverListMgrReloadTask = new CrossServerListMgrReloadTask();
            timerExecutorService.scheduleWithFixedDelay(serverListMgrReloadTask, 15, 10, TimeUnit.SECONDS);
        }

        if (null == crossUnionSyncTask){
            crossUnionSyncTask = new CrossUnionSyncTask();
            timerExecutorService.scheduleWithFixedDelay(crossUnionSyncTask, 15, 15, TimeUnit.SECONDS);
        }

//        if (null == goodsConfigReloadTask){
//            goodsConfigReloadTask = new CrossGoodsConfigReloadTask();
//            timerExecutorService.scheduleWithFixedDelay(goodsConfigReloadTask, 35, 10, TimeUnit.SECONDS);
//        }

        if (null == crossUnionRankMsgOverTask) {
            crossUnionRankMsgOverTask = new CrossUnionRankMsgOverTask();
            timerExecutorService.scheduleWithFixedDelay(crossUnionRankMsgOverTask, 60, 60, TimeUnit.SECONDS);
        }

        if (null == crossMonthAbilityRankUploadNotifyTask) {
            crossMonthAbilityRankUploadNotifyTask = new CrossMonthAbilityRankUploadNotifyTask();
            timerExecutorService.scheduleWithFixedDelay(crossMonthAbilityRankUploadNotifyTask, 30, 10, TimeUnit.SECONDS);
        }

        if (crossUnionActivityGroupCheckTask == null) {
            crossUnionActivityGroupCheckTask = new CrossUnionActivityGroupCheckTask();
            timerExecutorService.scheduleWithFixedDelay(crossUnionActivityGroupCheckTask, 30, 5, TimeUnit.SECONDS);
        }

        if (crossCricketCourtTask == null) {
            crossCricketCourtTask = new CrossCricketCourtTask();
            timerExecutorService.scheduleWithFixedDelay(crossCricketCourtTask, 5, 1, TimeUnit.SECONDS);
        }

        if (crossUnionCampWarGroupCheckTask == null) {
            crossUnionCampWarGroupCheckTask = new CrossUnionCampWarGroupCheckTask();
            timerExecutorService.scheduleWithFixedDelay(crossUnionCampWarGroupCheckTask, 30, 5, TimeUnit.SECONDS);
        }

        if (crossUnionCampWarSyncTask == null) {
            crossUnionCampWarSyncTask = new CrossUnionCampWarSyncTask();
            timerExecutorService.scheduleWithFixedDelay(crossUnionCampWarSyncTask, 30, 5, TimeUnit.SECONDS);
        }

        if (crossSystemLoopTask == null) {
            crossSystemLoopTask = new CrossSystemLoopTask();
            timerExecutorService.scheduleWithFixedDelay(crossSystemLoopTask, 30, 10, TimeUnit.SECONDS);
        }

        if (kuangCheckMatchGroupTask == null) {
            kuangCheckMatchGroupTask = new KuangCheckMatchGroupTask();
            timerExecutorService.scheduleWithFixedDelay(kuangCheckMatchGroupTask, 0, 10, TimeUnit.SECONDS);
        }

        if (kuangMatchGroupSyncTask == null) {
            kuangMatchGroupSyncTask = new KuangMatchGroupSyncTask();
            timerExecutorService.scheduleWithFixedDelay(kuangMatchGroupSyncTask, 10, 10, TimeUnit.SECONDS);
        }

        if(null == marriagePublishUploadTask){
            marriagePublishUploadTask = new CrossWorldMarriagePublishUploadTask();
            timerExecutorService.scheduleWithFixedDelay(marriagePublishUploadTask, 60, 60, TimeUnit.SECONDS);
        }

        if(null == systemGroupTask){
            systemGroupTask = new CrossSystemGroupTask();
            timerExecutorService.scheduleWithFixedDelay(systemGroupTask, 10, 10, TimeUnit.SECONDS);
        }

        if (null == cross3DataAnalyticsTask){
            cross3DataAnalyticsTask = new Cross3DataAnalyticsTask();
            timerExecutorService.scheduleWithFixedDelay(cross3DataAnalyticsTask, 0, 10, TimeUnit.SECONDS);
        }

        if (checkDeadLockTask == null) {
            checkDeadLockTask = new CheckDeadLockTask();
            timerExecutorService.scheduleWithFixedDelay(checkDeadLockTask, 0, 30, TimeUnit.SECONDS);
        }
        if (null == crossAutoLogSaveTask) {
            crossAutoLogSaveTask = new CrossAutoLogSaveTask();
            timerExecutorService.scheduleWithFixedDelay(crossAutoLogSaveTask, 60, 30, TimeUnit.SECONDS);
        }
        return true;
    }

    public static void stop(){
        cancelTask(mgrDataSaveTask);
        cancelTask(connectionCheckTask);
        cancelTask(directMemoryMonitorTask);
        cancelTask(checkDeadLockTask);
        cancelTask(cmdStatisticsTask);
        cancelTask(shuiHuYeZiCardMatchTask);
        cancelTask(shuiHuYeZiCardAutoPlayCardTask);
        cancelTask(logDataSaveCrossCenter2Task);
        cancelTask(unionTradeTask);
        cancelTask(unionFlagTask);
        cancelTask(unionDailyResetTask);
        cancelTask(serverListMgrReloadTask);
//        cancelTask(goodsConfigReloadTask);
        cancelTask(crossUnionActivityGroupCheckTask);
        cancelTask(crossUnionCampWarGroupCheckTask);
        cancelTask(crossUnionCampWarSyncTask);
        cancelTask(marriagePublishUploadTask);
        cancelTask(systemGroupTask);
        cancelTask(cross3DataAnalyticsTask);
    }

    private static void cancelTask(GeneralTask task) {
        if (null != task) {
            task.cancel(false);
        }
    }
}
